/*
 * Date：10/23/18 10:15 PM
 * Author: guomw
 *
 */

package org.elanfox.intellicommunity.biz.service.core.resident.impl;

import org.apache.commons.lang3.StringUtils;
import org.elanfox.intellicommunity.biz.dto.condition.OrgCondition;
import org.elanfox.intellicommunity.biz.dto.resident.OrganizationSaveDTO;
import org.elanfox.intellicommunity.biz.entity.resident.OrganizationPO;
import org.elanfox.intellicommunity.biz.entity.resident.OrganizationPO_;
import org.elanfox.intellicommunity.biz.repository.resident.OrganizationRepository;
import org.elanfox.intellicommunity.biz.service.core.resident.OrganizationService;
import org.elanfox.intellicommunity.biz.service.core.resident.WorkerService;
import org.elanfox.intellicommunity.data.base.base.AbstractSearchableCrudService;
import org.elanfox.intellicommunity.data.base.base.SpecificationAndSort;
import org.elanfox.intellicommunity.data.base.dto.BaseSaveDTO;
import org.elanfox.intellicommunity.data.base.dto.BaseSearchDTO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Sort;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;

import javax.persistence.criteria.Predicate;
import java.util.ArrayList;
import java.util.List;

/**
 * 用户相关service
 *
 * @author loutianxi
 * @date 2019-12-05
 */
@Service
public class OrganizationServiceImpl extends AbstractSearchableCrudService<OrganizationPO, Integer> implements OrganizationService {

    @Autowired
    private OrganizationRepository repository;
    @Autowired
    private WorkerService workerService;

    protected OrganizationServiceImpl(OrganizationRepository repository) {
        super(repository);
    }

    @Override
    protected SpecificationAndSort<OrganizationPO> specificationAndSort(BaseSearchDTO condition) {
        OrgCondition orgCondition = (OrgCondition) condition;
        Specification<OrganizationPO> specification = (root, query, cb) -> {
            List<Predicate> predicates = new ArrayList<>();
            if (StringUtils.isNotEmpty(orgCondition.getOrgName())) {
                predicates.add(cb.like(root.get(OrganizationPO_.name), likeAll(orgCondition.getOrgName())));
            }
            if (orgCondition.getDelFlag() != null) {
                predicates.add(cb.equal(root.get(OrganizationPO_.delFlag), orgCondition.getDelFlag()));
            }
            return cb.and(predicates.toArray(new Predicate[predicates.size()]));
        };
        Sort sort = new Sort(Sort.Direction.DESC, "createTime");
        return new SpecificationAndSort<>(specification, sort);
    }

    @Override
    public OrganizationPO save(BaseSaveDTO saveDTO) {
        OrganizationSaveDTO organizationSaveDTO = (OrganizationSaveDTO) saveDTO;
        OrganizationPO organizationPO = findById(organizationSaveDTO.getOrgId());
        if (organizationPO == null) {
            organizationPO = new OrganizationPO();
        }
        organizationPO.setName(organizationSaveDTO.getOrgName());
        organizationPO.setDescription(organizationSaveDTO.getDesc());
        return repository.save(organizationPO);
    }


    @Override
    public boolean del(Integer id) {
        //判断部门是否有工作人员
        long num = workerService.countByOrgId(id);
        if (num > 0) {
            throw new IllegalStateException("请先删除部门里的人员");
        }
        OrganizationPO organizationPO = findById(id);
        organizationPO.setDelFlag(false);
        save(organizationPO);
        return false;
    }
}
